package com.huawei.hms.update.ui;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build.VERSION;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.KeyEvent;
import com.huawei.hms.support.api.entity.hwid.HwIDConstant.ACTION;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.update.a.a.a;
import com.huawei.hms.update.a.a.b;
import com.huawei.hms.update.a.a.c;
import com.huawei.hms.update.a.a.d;
import com.huawei.hms.update.provider.UpdateProvider;
import com.huawei.hms.utils.PackageManagerHelper;
import com.huawei.hms.utils.Util;
import com.huawei.updatesdk.UpdateSdkAPI;
import com.huawei.updatesdk.service.appmgr.bean.ApkUpgradeInfo;
import com.huawei.updatesdk.service.otaupdate.CheckUpdateCallBack;
import com.huawei.updatesdk.service.otaupdate.UpdateKey;
import java.io.File;
import java.io.Serializable;

public class l extends a implements b {
    private a k;
    private c l;
    private int m = 0;

    private static Uri a(Context context, File file) {
        Object obj = 1;
        PackageManagerHelper packageManagerHelper = new PackageManagerHelper(context);
        String packageName = context.getPackageName();
        String str = packageName + UpdateProvider.AUTHORITIES_SUFFIX;
        if (VERSION.SDK_INT <= 23 || (context.getApplicationInfo().targetSdkVersion <= 23 && !packageManagerHelper.hasProvider(packageName, str))) {
            obj = null;
        }
        if (obj != null) {
            return UpdateProvider.getUriForFile(context, str, file);
        }
        return Uri.fromFile(file);
    }

    private static void a(final b bVar, final int i, final c cVar) {
        if (bVar != null) {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                public void run() {
                    bVar.a(i, cVar);
                }
            });
        }
    }

    public void onBridgeActivityCreate(Activity activity) {
        super.onBridgeActivityCreate(activity);
        if (this.c != null) {
            this.f = 6;
            if (!this.c.isNeedConfirm() || TextUtils.isEmpty(this.h)) {
                a(c.class);
                a((b) this);
                return;
            }
            a(i.class);
        }
    }

    public void onBridgeActivityDestroy() {
        g();
        super.onBridgeActivityDestroy();
    }

    public int getRequestCode() {
        return 2006;
    }

    public boolean onBridgeActivityResult(int i, int i2, Intent intent) {
        if (this.e && this.b != null) {
            return this.b.onBridgeActivityResult(i, i2, intent);
        }
        if (this.f != 6 || i != getRequestCode()) {
            return false;
        }
        if (a(this.g, this.i)) {
            b(0, this.f);
        } else {
            e();
        }
        return true;
    }

    public void a(int i, c cVar) {
        HMSLog.i("UpdateWizard", "Enter onCheckUpdate, status: " + d.a(i));
        switch (i) {
            case 1000:
                this.l = cVar;
                d();
                return;
            case 1201:
            case 1202:
            case 1203:
                a(j.b.class);
                return;
            default:
                a(j.b.class);
                return;
        }
    }

    private void d() {
        String netType;
        Activity b = b();
        String str = "";
        if (b != null) {
            netType = Util.getNetType(b.getBaseContext());
        } else {
            netType = "";
        }
        HMSLog.i("UpdateWizard", "current network is " + netType);
        if ("WIFI".equals(netType)) {
            a(e.class);
            f();
            HMSLog.i("UpdateWizard", "current network is wifi");
            return;
        }
        a(d.b.class);
        HMSLog.i("UpdateWizard", "current network is not wifi");
    }

    public void a(int i, int i2, int i3, File file) {
        HMSLog.i("UpdateWizard", "Enter onDownloadPackage, status: " + d.a(i) + ", reveived: " + i2 + ", total: " + i3);
        switch (i) {
            case 2000:
                c();
                if (file == null) {
                    e();
                    return;
                } else {
                    a(file);
                    return;
                }
            case 2100:
                if (this.d != null && (this.d instanceof e)) {
                    int i4;
                    if (i2 < 0 || i3 <= 0) {
                        i4 = 0;
                    } else {
                        i4 = (int) ((((long) i2) * 100) / ((long) i3));
                    }
                    this.m = i4;
                    ((e) this.d).b(i4);
                    return;
                }
                return;
            case 2201:
                a(j.c.class);
                return;
            case 2202:
                a(d.c.class);
                return;
            case 2203:
            case 2204:
                a(j.d.class);
                return;
            default:
                return;
        }
    }

    private void a(File file) {
        Context b = b();
        if (b != null && !b.isFinishing()) {
            Uri a = a(b, file);
            if (a == null) {
                HMSLog.e("UpdateWizard", "In startInstaller, Failed to creates a Uri from a file.");
                e();
                return;
            }
            Intent intent = new Intent(ACTION.HWID_SCHEME_URL);
            intent.setDataAndType(a, "application/vnd.android.package-archive");
            intent.setFlags(3);
            intent.putExtra("android.intent.extra.NOT_UNKNOWN_SOURCE", true);
            try {
                b.startActivityForResult(intent, getRequestCode());
            } catch (ActivityNotFoundException e) {
                HMSLog.e("UpdateWizard", "In startInstaller, Failed to start package installer." + e.getMessage());
                e();
            }
        }
    }

    public void a(b bVar) {
        HMSLog.i("UpdateWizard", "Enter onCancel.");
        if (bVar instanceof i) {
            a();
        } else if (bVar instanceof c) {
            g();
            a();
        } else if (bVar instanceof e) {
            g();
            a(d.d.class);
        } else if (bVar instanceof d.d) {
            a(e.class);
            f();
        } else if (bVar instanceof d.c) {
            a();
        } else if (bVar instanceof d.b) {
            a();
        } else {
            e();
        }
    }

    public void b(b bVar) {
        HMSLog.i("UpdateWizard", "Enter onDoWork.");
        if (bVar instanceof i) {
            bVar.c();
            a(c.class);
            a((b) this);
        } else if (bVar instanceof d.d) {
            bVar.c();
            a();
        } else if (bVar instanceof d.c) {
            a(e.class);
            f();
        } else if (bVar instanceof d.b) {
            a(e.class);
            f();
        } else if (bVar instanceof j.b) {
            e();
        } else if (bVar instanceof j.c) {
            e();
        } else if (bVar instanceof j.d) {
            e();
        }
    }

    private void e() {
        if (a(false)) {
            a(8, this.f);
        } else {
            b(8, this.f);
        }
    }

    void a(Class<? extends b> cls) {
        Exception e;
        c();
        try {
            b bVar = (b) cls.newInstance();
            if (!TextUtils.isEmpty(this.h) && (bVar instanceof i)) {
                ((i) bVar).a(this.h);
            }
            if (this.m > 0 && (bVar instanceof e)) {
                ((e) bVar).a(this.m);
            }
            bVar.a((a) this);
            this.d = bVar;
        } catch (InstantiationException e2) {
            e = e2;
            HMSLog.e("UpdateWizard", "In showDialog, Failed to show the dialog." + e.getMessage());
        } catch (IllegalAccessException e3) {
            e = e3;
            HMSLog.e("UpdateWizard", "In showDialog, Failed to show the dialog." + e.getMessage());
        } catch (IllegalStateException e4) {
            e = e4;
            HMSLog.e("UpdateWizard", "In showDialog, Failed to show the dialog." + e.getMessage());
        }
    }

    void a() {
        b(13, this.f);
    }

    private void a(final b bVar) {
        if (bVar != null) {
            Context b = b();
            if (b == null || b.isFinishing()) {
                a(bVar, 1201, null);
            } else {
                UpdateSdkAPI.checkTargetAppUpdate(b, this.c.b(), new CheckUpdateCallBack(this) {
                    final /* synthetic */ l b;

                    public void onUpdateInfo(Intent intent) {
                        if (intent != null) {
                            this.b.a(intent, bVar);
                        }
                    }

                    public void onMarketInstallInfo(Intent intent) {
                    }

                    public void onMarketStoreError(int i) {
                        HMSLog.e("UpdateWizard", "onMarketStoreError responseCode: " + i);
                    }

                    public void onUpdateStoreError(int i) {
                        HMSLog.e("UpdateWizard", "onUpdateStoreError responseCode: " + i);
                    }
                });
            }
        }
    }

    private void a(Intent intent, b bVar) {
        try {
            int intExtra = intent.getIntExtra(UpdateKey.STATUS, -99);
            HMSLog.i("UpdateWizard", "CheckUpdateCallBack status is " + intExtra);
            Object stringExtra = intent.getStringExtra(UpdateKey.FAIL_REASON);
            if (!TextUtils.isEmpty(stringExtra)) {
                HMSLog.e("UpdateWizard", "checkTargetAppUpdate reason is " + stringExtra);
            }
            if (intExtra == 7) {
                Serializable serializableExtra = intent.getSerializableExtra(UpdateKey.INFO);
                if (serializableExtra != null && (serializableExtra instanceof ApkUpgradeInfo)) {
                    ApkUpgradeInfo apkUpgradeInfo = (ApkUpgradeInfo) serializableExtra;
                    String package_ = apkUpgradeInfo.getPackage_();
                    int versionCode_ = apkUpgradeInfo.getVersionCode_();
                    Object downurl_ = apkUpgradeInfo.getDownurl_();
                    int size_ = apkUpgradeInfo.getSize_();
                    Object sha256_ = apkUpgradeInfo.getSha256_();
                    if (TextUtils.isEmpty(package_) || !package_.equals(this.c.b())) {
                        a(bVar, 1201, null);
                    } else if (versionCode_ < this.c.c()) {
                        HMSLog.e("UpdateWizard", "CheckUpdateCallBack versionCode is " + versionCode_ + "bean.getClientVersionCode() is " + this.c.c());
                        a(bVar, 1203, null);
                    } else if (TextUtils.isEmpty(downurl_) || TextUtils.isEmpty(sha256_)) {
                        a(bVar, 1201, null);
                    } else {
                        a(bVar, 1000, new c(package_, versionCode_, downurl_, size_, sha256_));
                    }
                }
            } else if (intExtra == 3) {
                a(bVar, 1202, null);
            } else {
                a(bVar, 1201, null);
            }
        } catch (Exception e) {
            HMSLog.e("UpdateWizard", "intent has some error" + e.getMessage());
            a(bVar, 1201, null);
        }
    }

    private void f() {
        Context b = b();
        if (b == null || b.isFinishing()) {
            a(j.c.class);
            return;
        }
        g();
        this.k = new com.huawei.hms.update.a.c(new com.huawei.hms.update.a.d(b));
        this.k.a(this, this.l);
    }

    private void g() {
        if (this.k != null) {
            this.k.a();
            this.k = null;
        }
    }

    public void onKeyUp(int i, KeyEvent keyEvent) {
        if (this.e && this.b != null) {
            this.b.onKeyUp(i, keyEvent);
        } else if (4 == i) {
            HMSLog.i("UpdateWizard", "In onKeyUp, Call finish.");
            Activity b = b();
            if (b != null && !b.isFinishing()) {
                b.setResult(0, null);
                b.finish();
            }
        }
    }
}
